package com.cindata

import grails.transaction.Transactional
import org.springframework.security.access.annotation.Secured

@Secured(['isAuthenticated()'])
@Transactional
class ContactImportController
{

    static allowedMethods = [save: "POST", update: "PUT", delete: "DELETE"]

    def springSecurityService

    @Transactional
    def index(Integer max)
    {
        def order = params["order"]
        def sort = params["sort"]
        params.max = Math.min(max ?: 10, 100)

        String sql = " from ContactImportBatch e "
        def user = User.findById(springSecurityService.principal.id)

        def accountSql = Account.FilterAccountSql(user)
        if (accountSql)
        {
            sql += " and e.createBy.account.id in ${accountSql}"
        }

        if (order && sort)
        {
            sql += " order by e.${sort} ${order}"
        }
        else
        {
            sql += " order by e.batchNumber desc "
        }

        def estateImportList = ContactImport.executeQuery(sql, [max: params.max ?: 10, offset: params.offset ?: 0])
        respond ContactImport.list(params), model: [estateImportCount: ContactImport.count(), estateImportBatchList: estateImportList, params: params]
    }

    @Transactional
    def searchImport()
    {
        def order = params["order"]
        def sort = params["sort"]
        def queryParam = new HashMap()
        //押品地址
        def user = User.findById(springSecurityService.principal.id)
        String sql = "from ContactImport e where 1=1  "
        def accountSql = Account.FilterAccountSql(user)
        if (accountSql)
        {
            sql += "and e.createBy.account.id in ${accountSql} "
        }
        if (params["id"])
        {
            def id = Long.parseLong(params["id"])
            sql += "and e.batch.id = :id "
            queryParam["id"] = id
        }
        if (params["batchs"])
        {
            sql += "and e.batch.batchNumber like :batchs "
            queryParam["batchs"] = "%" + params["batchs"] + "%"
        }
        if (params["createBy"])
        {
            sql += "and e.createBy.username = :createBy "
            queryParam["createBy"] = params["createBy"]
        }

        if (params["startDate"] && params["endDate"])
        {
            sql += "and e.startDate between to_date(:startDate,'yyyy年mm月dd日') and to_date(:endDate,'yyyy年mm月dd日') "
            queryParam["startDate"] = params["startDate"]
            queryParam["endDate"] = params["endDate"]
        }
        if (params["identityType"])
        {
            def identityType = Long.parseLong(params["identityType"])
            sql += " and e.identityType.id = :identityType "
            queryParam["identityType"] = identityType
        }
        if (params["address"])
        {
            sql += " and e.address like :address "
            queryParam["address"] = "%" + params["address"] + "%"
        }
        if (params["fullName"])
        {
            sql += " and e.fullName like :fullName "
            queryParam["fullName"] = "%" + params["fullName"] + "%"
        }
        if (params["idNumber"])
        {
            sql += " and e.idNumber like :idNumber "
            queryParam["idNumber"] = "%" + params["idNumber"] + "%"
        }
        if (params["cellphone"])
        {
            sql += " and e.cellphone like :cellphone "
            queryParam["cellphone"] = "%" + params["cellphone"] + "%"
        }
        if (params["bankAccountNo"])
        {
            sql += " and e.bankAccountNo like :bankAccountNo "
            queryParam["bankAccountNo"] = "%" + params["bankAccountNo"] + "%"
        }
        //执行查询方法
        def count = ContactImport.executeQuery("select count(e.id) " + sql, queryParam)
        def estateImportList
        if (count[0] > 0)
        {
            params.max = params.max ?: 10
            params.offset = params.offset ?: 0
            if (order && sort)
            {
                sql += " order by e.${sort} ${order}"
            }
            else
            {
                sql += " order by e.id desc "
            }
            estateImportList = ContactImport.executeQuery(sql, queryParam, [max: params.max ?: 10, offset: params.offset ?: 0])
        }
        if (estateImportList != null)
        {

            respond ContactImport, model: [estateImportList: estateImportList, estateImportCount: count[0], params: params], view: 'index'
        }
        else
        {
            respond ContactImport, view: 'index'
        }
    }

}
